package com.itheima.mm.service.impl;

import com.itheima.mm.constants.Constants;
import com.itheima.mm.dao.QuestionDao;
import com.itheima.mm.dao.ReviewDao;
import com.itheima.mm.pojo.ReviewLog;
import com.itheima.mm.service.IReviewService;
import com.itheima.mm.utils.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 包名:com.itheima.mm.service
 * 作者:Leevi
 * 日期2020-02-07  09:49
 */
@Service
public class ReviewService implements IReviewService {
    @Autowired
    private ReviewDao reviewDao;
    private QuestionDao questionDao;

    @Override
    @Transactional
    public boolean add(ReviewLog reviewLog) {
        reviewDao.add(reviewLog);

        //2. 修改t_question表中，该试题的status和review_status字段的值
        //判断review_status的值
        Map<String,Object> map = new HashMap<>();
        map.put("id",reviewLog.getQuestionId());//当前要修改的试题的id
        map.put("review_status",reviewLog.getStatus());//审核状态
        if (reviewLog.getStatus() == 1) {
            //表示审核通过，那么，我们就设置试题的status的值为"已发布"1
            map.put("status", Constants.QUESTION_PUBLISHED);//试题的发布状态为已发布

        }else {
            //表示审核不通过，那么，我们就设置试题的status的值为"已下架"0
            map.put("status", Constants.QUESTION_PRE_PUBLISH);//试题的发布状态为待发布
        }

        questionDao.updateQuestionStatus(map);
        return true;
    }
}
